

const actionJson ={}

const getActionArr = (routesArr, actionlist, userAction)=>{
    const arr = [...routesArr];
    routesArr.map((item, index) =>{
        if(item.children){
            arr[index]['hasMenu'] = hasMenu(item, actionlist, userAction);
            getActionArr(item.children, actionlist, userAction)
        }else{
            arr[index]['hasAction'] = hasAction(actionlist[item.actionKey], userAction);
            actionJson[item.key] = arr[index]['hasAction'];
        }

        
    })
    sessionStorage.setItem('actionJson', JSON.stringify(actionJson));
    return arr;
}

const hasMenu = (menu,actionlist,userAction)=>{
    let bol = false;
    const arr = menu.children
    
    for(let i=0;i<arr.length;i++){
        if(hasAction(actionlist[arr[i].actionKey], userAction)){
            return true;
        }
    }

    return false;
}

const hasAction = (actionlist, userAction)=>{
    const set = new Set(userAction);
    
    for(let i=0;i<actionlist.length;i++){
        if(set.has(actionlist[i].id)){
            return true;
        }
    }

    return false;
}

module.exports = {
    getActionArr,
    actionJson
}